package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

//    新增套餐
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result<String> addSetMeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐，{}", setmealDTO);
        setMealService.addSetmeal(setmealDTO);
        return Result.success();
    }

//    分页查询套餐
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询套餐，{}", setmealPageQueryDTO);
       PageResult pageResult = setMealService.page(setmealPageQueryDTO);
       return Result.success(pageResult);

    }

//    设置套餐状态
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status ,Long id) {
        setMealService.updateStatus(status,id);
        return Result.success();
    }

//    回显套餐信息
    @GetMapping("/{id}")
    public Result getSetMealById(@PathVariable Long id) {
        log.info("根据id查询套餐信息，{}",id);
        SetmealVO setmealVO = setMealService.getSetMealById(id);
        return Result.success(setmealVO);
    }


//    修改套餐内信息
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result updateSetMeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐信息{}", setmealDTO);
        setMealService.updateSetMeal(setmealDTO);
        return Result.success();
    }


//    批量删除套餐
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache" ,allEntries = true)
    public Result deleteSetMeal(@RequestParam List<Long> ids) {
        log.info("批量删除套餐的id，{}",ids);
        setMealService.deleteSetMeal(ids);
        return Result.success();
    }
}
